//
//  EScrollerView.m
//  icoiniPad
//
//  Created by Ethan on 12-11-24.
//
//

#import "UIImageView+AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "EScrollerView.h"

@interface EScrollerView()
{
    NSUInteger  _totalImgNumber;
}
@end

@implementation EScrollerView
@synthesize delegate,scrollView;

//- (void)dealloc {
////	[scrollView release];
////    [noteTitle release];
////	delegate=nil;
////    if (pageControl) {
////        [pageControl release];
////    }
////    if (imageArray) {
////        [imageArray release];
////        imageArray=nil;
////    }
////    if (titleArray) {
////        [titleArray release];
////        titleArray=nil;
////    }
////    [super dealloc];
//}

-(id)initWithFrameRect:(CGRect)rect ImageArray:(NSArray *)imgArr TitleArray:(NSArray *)titArr changPic:(BOOL)changPic
{
	if ((self=[super initWithFrame:rect])&&[imgArr count] != 0) {
        [self loadDataImageArray:imgArr TitleArray:titArr changPic:changPic];
	}
	return self;
}

- (void)loadDataImageArray:(NSArray *)imgArr TitleArray:(NSArray *)titArr changPic:(BOOL)changPic
{
    self.userInteractionEnabled=YES;
    
    if (titArr.count > 0) {        
        NSMutableArray *tempTitleArray=[NSMutableArray arrayWithArray:titArr];
        [tempTitleArray insertObject:[titArr objectAtIndex:([titArr count]-1)] atIndex:0];
        [tempTitleArray addObject:[titArr objectAtIndex:0]];
        titleArray=[NSArray arrayWithArray:tempTitleArray] ;
    }
    
    NSMutableArray *tempArray=[NSMutableArray arrayWithArray:imgArr];
    [tempArray insertObject:[imgArr objectAtIndex:([imgArr count]-1)] atIndex:0];
    [tempArray addObject:[imgArr objectAtIndex:0]];
    imageArray=[NSArray arrayWithArray:tempArray] ;
    
    viewSize=self.frame;
    NSUInteger pageCount=[imageArray count];
    _totalImgNumber = pageCount;
    scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, viewSize.size.width, viewSize.size.height)];
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(viewSize.size.width * pageCount, viewSize.size.height);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.scrollsToTop = NO;
    scrollView.delegate = self;
    for (int i=0; i<pageCount; i++) {
        
        NSString *imgURL=[imageArray objectAtIndex:i];

        if ([imgURL isKindOfClass:[NSNull class]]) {
            continue;
        }
//
//        if ([imgURL hasPrefix:@"/upload"]) {
////      imgURL = [NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,imgURL];
//        }
//        
       UIImageView *imgView=[[UIImageView alloc] init] ;
//        
//        if ([imgURL hasPrefix:@"http://"]) {
//            //网络图片 请使用ego异步图片库
//            [imgView sd_setImageWithURL:[NSURL URLWithString:imgURL]];
//            [imgView setNeedsDisplay];
//        }
//        else
//        {
//
            UIImage *img=[UIImage imageNamed:[imageArray objectAtIndex:i]];
          [imgView setImage:img];
//        }
       [imgView setFrame:CGRectMake(viewSize.size.width*i, 0,viewSize.size.width, viewSize.size.height)];
//        imgView.tag=i;
//        UITapGestureRecognizer *Tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagePressed:)] ;
//        [Tap setNumberOfTapsRequired:1];
//        [Tap setNumberOfTouchesRequired:1];
//        imgView.userInteractionEnabled=YES;
//        [imgView addGestureRecognizer:Tap];
        [scrollView addSubview:imgView];
//        
//        //标题
//        NSString *title = [titleArray objectAtIndex:i];
//        if ([title length] > 0) {
//            UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(10, imgView.bottom-29, 120, 24)];
//            lb.text = title;
//            lb.textAlignment = NSTextAlignmentLeft;
//            lb.textColor = [UIColor whiteColor];
//            lb.font = [UIFont boldSystemFontOfSize:14.];
//            [imgView addSubview:lb];
//        }
   }
//    
//    
//    
    
    [scrollView setContentOffset:CGPointMake(viewSize.size.width, 0)];
    [self addSubview:scrollView];
    
    
    float pageControlWidth=(pageCount-2)*10.0f+40.f;
    float pagecontrolHeight=20.0f;
    if ([imgArr count] > 1)
    {
        pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake((self.width-pageControlWidth-10),self.frame.size.height - 20, pageControlWidth, pagecontrolHeight)];
    }
    pageControl.currentPage=0;
    pageControl.numberOfPages=(pageCount-2);
    pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    pageControl.currentPageIndicatorTintColor = UIColorFromHex(0xbc0c16);
    [self addSubview:pageControl];
    
    if (changPic == YES && [imgArr count] > 1) {
        chang = YES;
        timer = [NSTimer scheduledTimerWithTimeInterval:3.5f target:self selector:@selector(changePicture) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
        
    }
}

- (void)changePicture
{
    long pageNum = pageControl.currentPage;
    CGSize myViewSize = scrollView.frame.size;
    CGRect rect = CGRectMake((pageNum + 2) * myViewSize.width, 0, myViewSize.width, myViewSize.height);
    [scrollView scrollRectToVisible:rect animated:YES];
    pageNum++;
    if (pageNum == imageArray.count - 2) {
        [scrollView setContentOffset:CGPointMake(0, 0)];
    }
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (chang == YES) {
        [timer invalidate];
    }
    
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (chang == YES) {
        timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(changePicture) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

    }
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    currentPageIndex=page;
    if (page == 0) {
        pageControl.currentPage = imageArray.count - 1;
    }else if (page == imageArray.count + 1)
    {
        pageControl.currentPage = 0;
    }
    pageControl.currentPage = page - 1;
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
    if (currentPageIndex==0) {
        
        [_scrollView setContentOffset:CGPointMake(([imageArray count]-2)*viewSize.size.width, 0)];
    }
    if (currentPageIndex==([imageArray count]-1)) {
        
        [_scrollView setContentOffset:CGPointMake(viewSize.size.width, 0)];
        
    }
}
- (void)imagePressed:(UITapGestureRecognizer *)sender
{
    if (sender.view.tag != 0 && sender.view.tag != _totalImgNumber - 1)
    {
        if ([delegate respondsToSelector:@selector(EScrollerViewDidClicked:)]) {
            [delegate EScrollerViewDidClicked:sender.view.tag];
        }
    }
}

@end
